package com.elegantmall.controller;


import cn.dev33.satoken.util.SaResult;
import com.elegantmall.dao.UserDao;
import com.elegantmall.pojo.GoodsPojo;
import com.elegantmall.pojo.UserOrderPojo;
import com.elegantmall.service.SellerServiceImpl;
import com.elegantmall.service.UserOrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/seller")
public class SellerController {
    @Autowired
    SellerServiceImpl sellerService;
    @Autowired
    private UserOrderServiceImpl userOrderService;
    @Autowired
    private UserDao userDao;

    // 新增库存管理接口
    @PostMapping("/goods/stock/increase")
    public SaResult increaseStock(
            @RequestParam String goodsId,
            @RequestParam int delta) {
        sellerService.increaseStock(goodsId, delta);
        return SaResult.ok("库存增加成功");
    }

    @PostMapping("/goods/stock/decrease")
    public SaResult decreaseStock(
            @RequestParam String goodsId,
            @RequestParam int delta) {
        sellerService.decreaseStock(goodsId, delta);
        return SaResult.ok("库存减少成功");
    }

    @PostMapping("/goods/stock/add/{seller_name}")
    public SaResult addGoods(
            @PathVariable String seller_name,
            @RequestBody GoodsPojo goodsPojo) {
        String result = sellerService.addGoods(seller_name, goodsPojo);
        return SaResult.ok(result);
    }

    @PostMapping("/goods/stock/delete/{seller_name}")
    public SaResult deleteGoods(
            @PathVariable String seller_name,
            @RequestParam String goodsId) {
        String result = sellerService.deleteGoods(seller_name, goodsId);
        return SaResult.ok(result);
    }

/*测试模块
    @GetMapping("/testStock")
    public String testStockOperations() {
        String testGoodsId = "test123";

        // 测试增加库存
        increaseStock("114514", 10);

        // 测试减少库存
        return decreaseStock("114514", 3);
    }*/

    //change
    @GetMapping("/orders/{seller_name}")
    public SaResult getOrderBySeller(@PathVariable String seller_name) {
        try {
            System.out.println("before name: " + seller_name);
            List<UserOrderPojo> orderList = userOrderService.getOrdersBySeller(seller_name);
            System.out.println("get list");
            for(int i=0;i<orderList.size();i++){
                UserOrderPojo order = orderList.get(i);
                try{
                    order.setAddress(userDao.getUserContactByUsername(order.getUsername()).getAddress());
                }catch(Exception e){}
                orderList.set(i, order);
            }
            System.out.println("before return");
            return SaResult.data(orderList);
        } catch (IllegalArgumentException e) {
            return SaResult.error(e.getMessage());
        } catch (Exception e) {
            return SaResult.error("获取商品列表失败");
        }
    }
    @GetMapping("/goods/{seller_name}")
    public SaResult getGoodsBySeller(@PathVariable String seller_name) {
        try {
            List<GoodsPojo> goodsList = sellerService.getGoodsBySeller(seller_name);
            return SaResult.data(goodsList);
        } catch (IllegalArgumentException e) {
            return SaResult.error(e.getMessage());
        } catch (Exception e) {
            return SaResult.error("获取商品列表失败");
        }
    }
    @PostMapping("goods/update")
    public SaResult updateGoods(
            @RequestBody GoodsPojo goodsPojo) {
        String result = sellerService.updateGoodsInfo(goodsPojo);
        return new SaResult().ok(result);
    }
}
